<template>
  <div>
    <el-form :model="form" :rules="rules" ref="registerinfo" label-width="80px" class="register-box">
      <h3 class="register-title ">注册信息</h3>
      <el-form-item label="用户名" prop="name">
        <el-input v-model="form.name" placeholder = "请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="passwd">
        <el-input type="password" v-model="form.passwd" placeholder = "请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder = "请输入性别" class="sex">
          <el-option v-for="(item,index) in sexList" :key="index" :value="item.value" :label="item.label"></el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="住址" prop="address">
        <el-input v-model="form.address" placeholder = "请输入住址"></el-input>
      </el-form-item>-->
      <el-form-item label="手机号" prop="iphone">
        <el-input v-model="form.iphone" placeholder = "请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker
          class="sex"
          value-format="yyyyMMdd"
          v-model="form.birthday"
          type="date">
        </el-date-picker>
        <!--<el-input v-model="form.birthday" placeholder = "请输入出生日期(yyyyMMdd)"></el-input>-->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('registerinfo')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>



<script>
import {register} from "../api/api";

export default {
  name: "registerinfo",
  data() {
    return {
      sexList: [{
        value:"男",
        lable:"0"
      },{
        value:"女",
        lable:"1"
      },{
        value:"人妖",
        lable:"2"
      }],
      form: {
        name: '',
        passwd: '',
        sex:'',
        address:'',
        iphone:'',
        birthday:''
      },
      rules: {
        name: [
          {required: true, message: '用户名不能为空', trigger: 'blur'}
        ],
        passwd: [
          {required: true, message: '密码不能为空', trigger: 'blur'}
        ],
        sex: [
          {required: true, message: '性别不能为空', trigger: 'blur'}
        ],
        /*address: [
          {required: true, message: '地址不能为空', trigger: 'blur'}
        ],*/
        iphone: [
          {required: true, message: '手机号不能为空', trigger: 'blur'}
        ],
        birthday: [
          {required: true, message: '出生日期不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let obj = this.sexList.find((item) =>{
            return item.value === this.form.sex
          });
          register(this.form.name,this.form.passwd,obj.lable,this.form.iphone,this.form.birthday).then(response =>{
            if(response.data.sysHead.retStatus == 'F'){
              alert('注册失败： '+response.data.sysHead.resultList[0].retMessage);
              this.form.name = null;
              this.form.passwd = null;
              this.form.sex = null;
              this.form.iphone = null;
              this.form.address = null;
              this.form.birthday = null;
            }else{
              alert('注册成功');
              this.$router.push('/');
            }
          }).catch(function (error) {
            alert('注册失败: '+error);
            console.log(error);
          })
        } else {
          alter('请填写完整信息');
        }
      })
    }
  }
}
</script>

<style scoped>
  .sex{
    width: 320px;
  }

  .register-box {
    /*宽度*/
    width: 400px;
    /*边距*/
    margin: 100px auto;
    /*边框*/
    border: 1px solid #2c3e50;
    /*内边距*/
    padding: 30px;
    /*边框圆角*/
    border-radius: 20px;
    /*阴影*/
    box-shadow: 0 0 30px #2c3e50;
  }

  .register-box {
  text-align: center;
  }
</style>
